查看原文
其他

手把手教你如何将项目发布到Maven中央仓库(附步骤及常见问题解决方法)

itmuch IT牧场 2019-07-12

点击上方 IT牧场 ,选择 置顶或者星标技术干货每日送达!

业余时间写了个轻量级的权限控制框架 light-security[1] ,并发布到了 Maven 中央仓库。发布时的操作步骤还挺多,我这个记性是记不住的,所以记录一下,便于以后查阅,也希望对大家有帮助。

一、Sonartype相关准备工作

1 前往 Sonartype[2] 注册账号,并记好账号和密码,后面有用

2 前往 Sonartype Dashboard[3] ,点击导航栏上的 Create 按钮,并按提示填写项目信息:

个人为项目 light-security-spring-boot-starter 填写的信息如 OSSRH-47914[4] 所示 ,供大家参考。

3 创建 Issue 后,等待审核即可。一般会在一个工作日内审核完成。当Issue的Status变为RESOLVED 或 FIXED 后,即可进行下一步操作

二、GPG相关准备工作

2.1 安装GPG

Mac安装GPG:

brew install gpg

Ubuntu安装GPG:

sudo apt-get install gnupg

2.2 GPG常用命令

gpg --version 检查安装成功没gpg --gen-key 生成密钥对gpg --list-keys 查看公钥gpg --keyserver 服务器地址 --send-keys 公钥ID 将公钥发布到 PGP 密钥服务器gpg --keyserver 服务器地址 --recv-keys 公钥ID 查询公钥是否发布成功

2.3 生成秘钥

gpg --gen-key按照提示输入姓名/邮箱,然后按O即可生成。

如果遇到问题,可详见”遇到的问题一节”。

2.4 查看本地秘钥

gpg --list-keys

结果类似如下:

➜ ~ gpg --list-keys/Users/itmuch.com/.gnupg/pubring.kbx------------------------------------pub rsa2048 2019-04-20 [SC] [有效至:2021-04-19] [xxxxxxxxx]uid [ 绝对 ] itmuch.com <eacdy0000@126.com>sub rsa2048 2019-04-20 [E] [有效至:2021-04-19]

三、配置Maven

TIPS

可参考官方文档配置https://central.sonatype.org/pages/apache-maven.html

1 修改项目的pom.xml,添加如下内容:

<licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> </license></licenses>
<developers> <developer> <name>itmuch</name> <email>eacdy0000@126.com</email> <url>https://github.com/eacdy</url> </developer></developers>
<scm> <url>https://github.com/eacdy/light-security</url> <connection>https://github.com/eacdy/light-security.git</connection> <developerConnection>https://github.com/eacdy</developerConnection></scm>
<distributionManagement> <snapshotRepository> <id>ossrh</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>ossrh</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository></distributionManagement>
<profiles> <profile> <id>release</id> <build> <plugins> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.7</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5</version> <configuration> <autoVersionSubmodules>true</autoVersionSubmodules> <useReleaseProfile>false</useReleaseProfile> <releaseProfiles>release</releaseProfiles> <goals>deploy</goals> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile></profiles>

可参考我的配置:Light Security Spring Boot Starter Pom.xml[5]

2 修改 $MAVEN_HOME/conf/settings.xml 文件(即你的Maven配置文件),添加如下内容:

<server> <!-- 这里的ID要和distributionManagement.repository.id一致 --> <id>ossrh</id> <!-- https://issues.sonatype.org/的账号 --> <username>账号</username> <!-- https://issues.sonatype.org/的密码 --> <password>密码</password></server>

四、修改项目版本

用如下命令,修改项目的版本,例如1.0.1-RELEASE

mvn versions:set -DnewVersion=1.0.1-RELEASE

当然也可手动修改版本,不过当项目比较复杂,module比较多时,手动修改就会比较麻烦,而且容易出错。建议用命令修改。

五、发布

执行如下命令即可将依赖发布到中央仓库。

mvn clean install deploy -P release

不出意外,构建会报xxx服务器无法找到GPG的异常。原因是前文生成的秘钥尚未发布到key server。keyserver的地址会在异常中打印出来。我的项目报的是 http://keys.gnupg.net:11371/ 。于是执行

gpg --keyserver http://keys.gnupg.net:11371/ --send-keys [xxxxxxxxx]其中的[xxxxxxxxx],可用gpg --list-keys显示出来。

然后再次执行如下命令:

mvn clean install deploy -P release

此时即可发布成功。发布完使用如下命令重置为SNAPSHOT版本

mvn versions:set -DnewVersion=1.0.2-SNAPHOST

六、遇到的问题

6.1 执行 gpg --gen-key 报 Key generation failed: Timeout 的异常

解决方案:

rm -rf ~/.gnupggpg --gen-key

6.2 执行mvn clean install deploy -P release 时,报gpg: signing failed: Inappropriate ioctl for device

原因是当前终端无法弹出密码输入页面。

解决方案:

export GPG_TTY=$(tty)mvn clean install deploy -P release

6.3 连不上 https://oss.sonatype.org

•科学上网(在某些城市有被查水表、罚款的风险),自己找梯子吧;•飞到香港、澳门或者海外等能没有墙的地方,然后发布应用,发布完再回国(一种人傻钱多的方式);•移民(更彻底的解决方案,但如果想看抗日神剧或者听某些国内音乐,可能要用梯子翻回来)……

TIPS

如果你在发布时遇到其他问题,也可添加我的微信 jumping_me ,我尽量帮助到你。

七、参考文档

•官方文档[6]•如何发布jar包到maven中央仓库[7]•发布项目至maven中央仓库汇总(流程+问题)[8]•向maven中央仓库提交jar[9]•gpg: signing failed: Inappropriate ioctl for device[10]•Git 生成GPG key 报错 Key generation failed: Timeout 的解决方法[11]

干货分享

最近将个人学习笔记整理成册,使用PDF分享。关注我,回复如下代码,即可获得百度盘地址,无套路领取!

•001:《Java并发与高并发解决方案》学习笔记;•002:《深入JVM内核——原理、诊断与优化》学习笔记;•003:《Java面试宝典》•004:《Docker开源书》•005:《Kubernetes开源书》•006:《DDD速成(领域驱动设计速成)》•007:全部

近期热文

Spring Cloud Alibaba系列教程-04-使用Nacos管理配置Netflix时代之后Spring Cloud微服务的未来盘点Git的那些冷门玩法6到飞起的Java诊断工具ArthasSpring Cloud学习资源一网打尽!Awesome Spring Cloud v1.0干货|Spring Cloud Bus 消息总线介绍分享:个人是怎么学习新知识的

关注我

References

[1] light-security: https://www.github.com/eacdy/light-security
[2] Sonartype: https://issues.sonatype.org/
[3] Sonartype Dashboard: https://issues.sonatype.org/secure/Dashboard.jspa
[4] OSSRH-47914: https://issues.sonatype.org/browse/OSSRH-47914
[5] Light Security Spring Boot Starter Pom.xml: https://github.com/eacdy/light-security/blob/master/light-security-spring-boot-starter/pom.xml
[6] 官方文档: https://central.sonatype.org/pages/apache-maven.htmll
[7] 如何发布jar包到maven中央仓库: https://www.cnblogs.com/softidea/p/6743108.html
[8] 发布项目至maven中央仓库汇总(流程+问题): https://blog.csdn.net/z69183787/article/details/81981423
[9] 向maven中央仓库提交jar: https://www.cnblogs.com/gaoxing/p/4359795.html
[10] gpg: signing failed: Inappropriate ioctl for device: https://blog.csdn.net/weixin_33924312/article/details/88146168
[11] Git 生成GPG key 报错 Key generation failed: Timeout 的解决方法: https://blog.csdn.net/everblog/article/details/79031243

点"在看"是一种美德^_^

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存